// 获取轮播图元素
const carouselImages = document.querySelectorAll('.carousel-images img');
const carouselTabs = document.querySelectorAll('.carousel-tabs .tab');
let currentIndex = 0;

// 显示当前图片和对应的 Tab
function showImage(index) {
  carouselImages.forEach((img, i) => {
    if (i === index) {
      img.classList.add('active');
    } else {
      img.classList.remove('active');
    }
  });

  carouselTabs.forEach((tab, i) => {
    if (i === index) {
      tab.classList.add('active');
    } else {
      tab.classList.remove('active');
    }
  });
}

// 自动轮播
function autoPlay() {
  currentIndex = (currentIndex + 1) % carouselImages.length;
  showImage(currentIndex);
}

let autoPlayInterval = setInterval(autoPlay, 3000);

// 鼠标悬停暂停轮播
const carousel = document.querySelector('.carousel');
carousel.addEventListener('mouseover', function () {
  clearInterval(autoPlayInterval);
});

carousel.addEventListener('mouseout', function () {
  autoPlayInterval = setInterval(autoPlay, 3000);
});

// 鼠标悬停 Tab 切换图片
carouselTabs.forEach((tab, index) => {
  tab.addEventListener('mouseover', function () {
    currentIndex = index;
    showImage(currentIndex);
  });
});
